<template>
  <div class="home-product">
    <HomePanel
      :title="cate.name"
      v-for="cate in goodsProductArr"
      :key="cate.id"
    >
      <template #main>
        <div class="container">
          <RouterLink class="cover" to="/">
            <!--自定义图片懒加载 -->
            <img v-img-lazy="cate.picture" />
            <strong class="label">
              <span>{{ cate.name }}馆</span>
              <span>{{ cate.saleInfo }}</span>
            </strong>
          </RouterLink>
          <ul class="goods-list">
            <li v-for="good in cate.goods" :key="good.id">
              <!-- 优化 -->
              <GoodsItem :good="good"></GoodsItem>
            </li>
          </ul>
        </div>
      </template>
    </HomePanel>
  </div>
</template>

<script setup lang="ts">
import { defineOptions, ref, onMounted } from "vue";
//引入公用插槽模版
import HomePanel from "./HomePanel.vue";
import GoodsItem from "./GoodsItem.vue";
import { reqGetGoodsList } from "@/apis/home";
defineOptions({
  name: "HomeProduct",
});
//存储已有所有产品的数据的数组
let goodsProductArr = ref([]);
onMounted(() => {
  getGoodsList();
});
//获取已有所有产品的数据
const getGoodsList = async () => {
  let res: any = await reqGetGoodsList();
  goodsProductArr.value = res.result;
};
</script>

<style scoped lang="scss">
.home-product {
  background-color: #fff;
  margin-top: 20px;
  .container {
    display: flex;
    .cover {
      position: relative;
      width: 240px;
      height: 610px;
      img {
        width: 100%;
        height: 100%;
      }
      .label {
        display: flex;
        position: absolute;
        left: 0;
        top: 50%;
        width: 220px;
        height: 66px;
        line-height: 66px;
        color: #fff;
        font-weight: normal;
        transform: translate3d(0, -50%, 0);
        span {
          text-align: center;
          &:first-child {
            width: 76px;
            background: rgba(0, 0, 0, 0.9);
          }
          &:last-child {
            flex: 1;
            background: rgba(0, 0, 0, 0.7);
          }
        }
      }
    }
    .goods-list {
      display: flex;
      flex-wrap: wrap;
      width: 990px;
      margin-left: 10px;
      li {
        width: 240px;
        height: 300px;
        margin: 0 10px 10px 0;
        &:nth-child(4n) {
          margin-right: 0;
        }
      }
    }
  }
}
</style>
